package org.feng.controller;

import org.feng.pojo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * redis监控
 */
@RestController
@RequestMapping( "/system/redis")
@AllArgsConstructor
@Api(value = "redis监控", tags = "redis监控")
public class RedisController {

    private final RedisTemplate redisTemplate;

    @ApiOperation(value = "redis监控数据", notes = "redis监控数据")
    @GetMapping()
    public Result<HashMap<String, Object>> list() {
        HashMap<String, Object> reMap = new HashMap<>(2);
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        Properties info = redisTemplate.getRequiredConnectionFactory().getConnection().info("all");
        Properties commandStats = redisTemplate.getRequiredConnectionFactory().getConnection().info("commandstats");
        if (info != null && commandStats != null) {
            String aofEnabled = info.getProperty("aof_enabled");
            list.add(getMap("redis版本", info.getProperty("redis_version")));
            list.add(getMap("端口", info.getProperty("tcp_port")));
            list.add(getMap("宿主机", info.getProperty("os")));
            list.add(getMap("角色", info.getProperty("role")));
            String clusterEnabled = info.getProperty("cluster_enabled");
            String slaves = info.getProperty("connected_slaves");
            list.add(getMap("从机数", slaves));
            if (clusterEnabled.equals("0") && slaves.equals("0")) {
                list.add(getMap("模式", "单机"));
            } else {
                list.add(getMap("模式", "主从/集群"));
            }
            list.add(getMap("已执行命令数", info.getProperty("total_commands_processed")));
            list.add(getMap("命中key数", info.getProperty("keyspace_hits")));
            list.add(getMap("未命中key数", info.getProperty("keyspace_misses")));
            list.add(getMap("运行时长", info.getProperty("uptime_in_days") + "天"));
            list.add(getMap("内存占用", info.getProperty("mem_fragmentation_ratio") + "%"));
            list.add(getMap("aof状态", aofEnabled.equals("0") ? "关闭" : "开启"));
            list.add(getMap("客户端连接数", info.getProperty("connected_clients")));
            Set<Object> set = commandStats.keySet();
            ArrayList<Map<String, Object>> keyList = new ArrayList<>();
            for (Object o : set) {
                String key = o.toString();
                String property = commandStats.getProperty(key);
                String[] split = property.split(",");
                keyList.add(getMap(key.split("_")[1], split[0].substring(6)));
            }
            keyList.forEach(item->{
                for (String s : item.keySet()) {
                    item.put("value",Integer.valueOf(item.get("value").toString()));
                }
            });

            reMap.put("info", list);
            reMap.put("key", keyList);
            reMap.put("memory", info.getProperty("mem_fragmentation_ratio"));
            return Result.success(reMap);
        }
        return Result.success(reMap);

    }

    private Map<String, Object> getMap(String name, String value) {
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("name", name);
        map.put("value", value);
        return map;
    }
}
